package com.ydgk.tool;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.UUID;

public class Tool {
    public static final int MACHINE_ID = 1;
    public static String upload(HttpServletRequest request, MultipartFile file, String UPLOAD_DIRECTORY) throws IOException {
        ServletContext application = request.getServletContext();
        String uploadPath = application.getRealPath(UPLOAD_DIRECTORY);
        // 如果目录不存在则创建
        File uploadDir = new File(uploadPath);
        if (!uploadDir.exists()) {
            uploadDir.mkdirs();//建upload文件
        }
        String fileName = file.getOriginalFilename(); //上传的文件名字
        int index = fileName.lastIndexOf(".");
        String newfilename = Math.random() + fileName.substring(index);
        File f = new File(uploadPath + "/" + newfilename);
        // 保存文件到硬盘
        InputStream is = file.getInputStream();
        FileOutputStream os = new FileOutputStream(f);
        byte[] b = new byte[1024];
        int len = 0;
        while ((len = is.read(b)) != -1) {
            os.write(b, 0, len);
        }
        os.close();
        is.close(); //文件写入
        //保存到数据库的图片路径是相对路径,不能用绝对路径
        return newfilename;
    }

    public static String getPageContent(Integer pageNo , Integer pageTotal , String type){
        StringBuilder pageContent = new StringBuilder();
        if(pageNo == 1) {
            pageContent.append("<li class='page-item disabled'><a class='page-link' href='#' tabindex='-1' aria-disabled='true'><i class='ti-angle-left'></i></a></li>");
        }else {
            pageContent.append("<li class='page-item'><a class='page-link' onclick='"+type+"FurnitureCards("+(pageNo-1)+")'><i class='ti-angle-left'></i></a></li>");
        }

        if(pageTotal > 5){
            if(pageNo == 4){
                pageContent.append("<li class='page-item'><a class='page-link' onclick='"+type+"FurnitureCards("+1+")'>"+1+"</a></li>");
            }
            if(pageNo == 5){
                pageContent.append("<li class='page-item'><a class='page-link' onclick='"+type+"FurnitureCards("+1+")'>"+1+"</a></li>");
                pageContent.append("<li class='page-item'><a class='page-link' onclick='"+type+"FurnitureCards("+2+")'>"+2+"</a></li>");
            }
            if(pageNo > 5){
                pageContent.append("<li class='page-item'><a class='page-link' onclick='"+type+"FurnitureCards("+1+")'>"+1+"</a></li>");
                pageContent.append("<li class='page-item disabled'><a class='page-link' href='#' tabindex='-1' aria-disabled='true'>...</a></li>");
            }
        }

        for (int i = pageNo - 2; i <= pageNo + 2; i++) {
            if (i < 1 || i > pageTotal) {
                continue;
            }
            if (i == pageNo) {
                pageContent.append("<li class='page-item active' aria-current='page'><a class='page-link' onclick='"+type+"FurnitureCards("+i+")'>"+i+"<span class='sr-only'>(current)</span></a></li>");
            } else {
                pageContent.append("<li class='page-item'><a class='page-link' onclick='"+type+"FurnitureCards("+i+")'>"+i+"</a></li>");
            }
        }
        if(pageTotal > 5){
            if(pageNo < (pageTotal - 4)){
                pageContent.append("<li class='page-item disabled'><a class='page-link' href='#' tabindex='-1' aria-disabled='true'>...</a></li>");
                pageContent.append("<li class='page-item'><a class='page-link' onclick='"+type+"FurnitureCards("+pageTotal+")'>"+pageTotal+"</a></li>");
            }

            if(pageNo == (pageTotal - 3)){
                pageContent.append("<li class='page-item'><a class='page-link' onclick='"+type+"FurnitureCards("+pageTotal+")'>"+pageTotal+"</a></li>");
            }
            if(pageNo == (pageTotal - 4)){
                pageContent.append("<li class='page-item'><a class='page-link' onclick='"+type+"FurnitureCards("+(pageTotal - 1)+")'>"+(pageTotal - 1)+"</a></li>");
                pageContent.append("<li class='page-item'><a class='page-link' onclick='"+type+"FurnitureCards("+pageTotal+")'>"+pageTotal+"</a></li>");
            }
        }

        if(pageNo == pageTotal) {
            pageContent.append("<li class='page-item disabled'><a class='page-link' href='#' tabindex='-1' aria-disabled='true'><i class='ti-angle-right'></i></a></li>");
        }else {
            pageContent.append("<li class='page-item'><a class='page-link' onclick='"+type+"FurnitureCards("+(pageNo+1)+")'><i class='ti-angle-right'></i></a></li>");
        }

        return pageContent.toString();
    }

    public static String getPageContentForManager(Integer pageNo , Integer pageTotal , String methodName){
        StringBuilder pageContent = new StringBuilder();
        if(pageNo == 1) {
            pageContent.append("<li class=\"page-item disabled\"><a class=\"page-link\" tabindex=\"-1\" aria-disabled=\"true\">首页</a></li>");
        }else {
            pageContent.append("<li class=\"page-item \"><a class=\"page-link\" onclick='"+methodName+"(1)' >首页</a></li>");
        }


        for (int i = pageNo - 2; i <= pageNo + 2; i++) {
            if (i < 1 || i > pageTotal) {
                continue;
            }
            if (i == pageNo) {
                pageContent.append("<li class=\"page-item active\" aria-current=\"page\"><a class=\"page-link\">"+i+"<span class=\"sr-only\">(current)</span></a></li>");
            } else {
                pageContent.append("<li class=\"page-item\"><a class=\"page-link\" onclick='"+methodName+"("+i+")'>"+i+"</a></li>");
            }
        }


        if(pageNo == pageTotal) {
            pageContent.append("<li class=\"page-item disabled\"><a class=\"page-link\" tabindex=\"-1\" aria-disabled=\"true\">尾页</a></li>");
        }else {
            pageContent.append("<li class=\"page-item \"><a class=\"page-link\" onclick='"+methodName+"("+pageTotal+")' >尾页</a></li>");
        }
        return pageContent.toString();
    }

    public static String getPageContentForPortal(Integer pageNo , Integer pageTotal , String methodName){
        StringBuilder pageContent = new StringBuilder();
        if(pageNo == 1) {
            pageContent.append("<span class=\"disabled\" title=\"首页\">首页</span>");
        }else {
            pageContent.append("<span onclick='"+methodName+"(1)' title=\"首页\">首页</span>");
        }


        for (int i = pageNo - 2; i <= pageNo + 2; i++) {
            if (i < 1 || i > pageTotal) {
                continue;
            }
            if (i == pageNo) {
                pageContent.append("<span onclick='"+methodName+"("+i+")' class=\"current\">"+i+"</span>");
            } else {
                pageContent.append("<span onclick='"+methodName+"("+i+")'>"+i+"</span>");
            }
        }


        if(pageNo == pageTotal) {
            pageContent.append("<span class=\"disabled\" title=\"尾页\">尾页</span>");
        }else {
            pageContent.append("<span onclick='"+methodName+"("+pageTotal+")' title=\"尾页\">尾页</span>");
        }
        return pageContent.toString();
    }

    public static String getUUID(){
        return UUID.randomUUID().toString().replace("-", "").substring(0, 6);
    }

    public static String getOrderNumber() {
        int machineId = MACHINE_ID;
        int hashCodeV = UUID.randomUUID().toString().hashCode();
        System.out.println("hashCodeV：" + hashCodeV);
        if (hashCodeV < 0) {
            hashCodeV = -hashCodeV;
        }
        // 0 代表前面补充0
        // 4 代表长度为4
        // d 代表参数为正整型
        //edit---20181203 hzj
        return machineId + String.format("%015d", hashCodeV);
    }

}
